/*
 * $Id: command.c 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 *
 * Copyright (c) 2006, 2007, 2008 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "dictc.h"
#include "util.h"

#define CRLFLEN 3

void
print_help()
{
   warning("options: ") ;
   printf("\t\t -d [ turn on debug option ]\n");
   printf("\t\t -s [ name of the dict server ]\n");
   printf("\t\t -p [ port number of the dict server - default 2628 ]\n");
   printf("\n\ncommands for CLI: \n");
   printf("define: \n");
   printf("\t usage: define [ name of the database ] word\n");
   printf("\t\t databases can be a name or * or ! sign\n");
   printf("match: \n");
   printf("\t usage: match [ name of the databse ] [ a strategy ] word\n");
   printf("show: \n");
   printf("\t usage: show [ databases | strategies | server ]\n");
   printf("\t usage: show info [ name of a database ]\n");
   printf("option: \n");
   printf("\t this command is not implemented yet\n");
   printf("client: \n");
   printf("\t: usage: client [ any information about this client ]");
   printf("auth: \n");
   printf("\t this command is not implemented yet\n");
   printf("status: \n");
   printf("\t command takes no parameters, returns status of the server\n");
   printf("quit: \n");
   printf(
     "\t command takes no parameters, quits active TCP dict client sessions\n");
}

void
write_command(const char *cmd) 
{
   if (cmd) {
      size_t length = strlen(cmd) + CRLFLEN;
      char bufline[length];
      memset(bufline, '\0', length);
      snprintf(bufline, sizeof(bufline), "%s\r\n", cmd);
      sock_write(bufline);
   }
}

void
cmd_define(int argc, char *cmdline)
{
   if (dclient->conn) {
      if (argc <= 1) {
         warning("usage: define [ name of the database ] word");
         warning("usage: databases can be a name or * sign or ! sign");
      }
      else {
         if (cmdline) 
            write_command(cmdline);
         else  {
            warning("%s(): no CLI arguments to process", __func__);
            warning("usage: define [ name of the database ] word");
            warning("usage: databases can be a name or * sign or ! sign");
         }
      }
   }
   else 
      warning("%s(): must be connected first", __func__);
}

void
cmd_match(int argc, char *cmdline)
{
   if (dclient->conn) {
      if (argc <= 1)
         warning("usage: match [ name of the database ] [ a strategy ] word");
      else {
         if (cmdline) 
            write_command(cmdline);
         else {
            warning("%s(): no CLI arguments to process", __func__);
            warning(
                  "usage: match [ name of the database ] [ a strategy ] word");
         }
      }
   }
   else
      warning("%s(): must be connected first", __func__);
}

void
cmd_show(int argc, char *cmdline)
{
   if (dclient->conn) {
      if ((argc <= 1) || (argc > 3)) {
         warning("usage: show [ databases | strategies | server ]");
         warning("usage: show info [ name of the database ]");
      }
      else {
         if (cmdline) 
            write_command(cmdline);
         else {
            warning("%s(): no CLI arguments to process", __func__);
            warning("usage: show [ databases | strategies | server ]");
            warning("usage: show info [ name of the database ]");
         }
      }
   }
   else
      warning("%s(): must be connected first", __func__);
}

void
cmd_option(int argc, char *cmdline)
{
   if (dclient->conn) {
      if (argc >= 1)
         write_command(cmdline);
   }
   else
      warning("%s(): must be connected first", __func__);
}

void
cmd_client(int argc, char *cmdline)
{
   if (dclient->conn) {
      if (argc <= 1)
         warning("usage: client [ any information about this client ]");
      else {
         if (cmdline)
            write_command(cmdline);
         else {
            warning("%s(): no CLI arguemnts to process", __func__);
            warning("usage: client [ any information about this client ]");
         }
      }
   }
   else
      warning("%s(): must be connected first", __func__);
}

void
cmd_auth(int argc, char *cmdline)
{
   /* XXX: Need more reading as to what this command actually does
    * just a stub code lines here to make gcc -Wall switch happy.
    * please refrain from using this command
    */
   if (argc >= 1)
      write_command(cmdline);
}

void
cmd_status(int argc, char *cmdline)
{
   if (dclient->conn) {
      if (argc && (argc == 1))
         write_command(cmdline);
      else
         warning("%s(): extra parameters in CLI", __func__);
   }
   else
      warning("%s(): must be connectes first", __func__);
}

void
cmd_help(int argc, char *cmdline)
{
   if (dclient->conn) {
      if (argc && (argc == 1))
         write_command(cmdline);
      else
         warning("%s(): extra parameters in CLI", __func__);
   }
   else 
      print_help();
}

void
cmd_quit(int argc, char *cmdline)
{
   if (dclient->conn) {
      conn_quit = TRUE;
      if (argc && (argc == 1)) {
         write_command(cmdline);
      }
      else
         warning("%s(): extra parameters in CLI", __func__);
   }
   else
      warning("%s(): must be connected first", __func__);
}
